home *** CD-ROM | disk | FTP | other *** search
- Path: lrz-muenchen.de!news
- From: watzka@stat.uni-muenchen.de (Kurt Watzka)
- Newsgroups: comp.lang.c
- Subject: Re: returning address of value to main
- Date: 11 Apr 1996 09:37:05 GMT
- Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
- Distribution: world
- Message-ID: <4kijs1$je1@sparcserver.lrz-muenchen.de>
- References: <4khpjq$a3c@pipe1.nyc.pipeline.com>
- NNTP-Posting-Host: sun2.lrz-muenchen.de
-
- luciferm@nyc.pipeline.com (manjila thapa) writes:
-
- >I want to return the address of element of an array
- >a[0]....a[n]....a[m-1] (address of a[n] is to be returned)
- >from a function to the main so that:
-
- >main(){
- >double *newarr;
- >...
- >...
- >newarr= function(......) /*function is supposed to return address*/
- >__________________________________________
- >i tried
-
- >return &a[n];
-
- >in function but it doesn't work! Isn't this the right way to get the
- >address? (this is part of my hw, hope u don't mind)
-
- "Doesn't work" is a weak description of a problem, imho. "&a[n]" and
- "a + n" both give you the address of the n + 1 th element of the
- array, but you could have found this information in any textbook
- on C. The question is whether the memory location referred by
- "a + n" is still valid after you return form your function. This
- is not the case for an array with storage class "auto" in a
- function.
-
- Kurt
- --
- | Kurt Watzka Phone : +49-89-2180-6254
- | watzka@stat.uni-muenchen.de
-
-